<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="标题">
          <el-input
            v-model="formInline.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否上架">
          <el-select
            style="width: 160px"
            v-model="formInline.push"
            placeholder="请选择"
            clearable
          >
            <el-option label="否" :value="0"></el-option>
            <el-option label="是" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="onSubmit()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-button
      style="margin-bottom: 16px"
      size="small"
      type="primary"
      icon="Plus"
      @click="addAnnouncement"
      >添加公告</el-button
    >
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="title" label="标题"></el-table-column>
          <el-table-column prop="content" label="公告内容"></el-table-column>
          <el-table-column label="是否上架">
            <template #default="scope">
              <el-tag :type="scope.row.push == 1 ? 'success' : 'danger'">{{
                scope.row.push == 1 ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template #default="scope">
              <el-button @click="edit(scope.row)" type="text" size="small"
                >编辑</el-button
              >
              <el-button
                v-if="scope.row.push == 1"
                @click="pushOrDown(scope.row)"
                type="text"
                size="small"
                >下架</el-button
              >
              <el-button
                v-if="scope.row.push == 0"
                @click="pushOrDown(scope.row)"
                type="text"
                size="small"
                >上架</el-button
              >
              <el-button @click="del(scope.row)" type="text" size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-sizes="[2, 10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <el-dialog title="添加公告" v-model="dialogVisible">
      <el-form
        size="small"
        :model="form"
        :rules="formRules"
        :label-width="80"
        ref="form"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题"></el-input>
        </el-form-item>

        <el-form-item label="公告内容" prop="content">
          <el-input
            type="textarea"
            v-model.number="form.content"
            placeholder="请输入标题"
            :rows="4"
          ></el-input>
        </el-form-item>

        <el-form-item label="是否发布" prop="push">
          <el-radio-group v-model="form.push">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            type="primary"
            @click="confirmAnnouncement"
            :loading="formLoading"
            >确 定</el-button
          >
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import AnnouncementApi from "@/api/announcement.js";
export default {
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      plateStatus: -1,
      /*横向表单数据模型*/
      formInline: {
        title: "",
        push: "",
      },
      form: {
        title: "",
        content: "",
        push: "",
      },
      dialogVisible: false,
      formLoading: false,
      isEdit: false,
      formRules: {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        push: [
          {
            required: true,
            message: "请选择是否发布",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    pushOrDown(row) {
      const text = row.push == 1 ? "下架" : "上架";
      ElMessageBox.confirm(`确定${text}吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        const params = {
          push: 1 - row.push,
          id: row.id,
        };
        AnnouncementApi.pushOrDown(params).then((res) => {
          ElMessage.success(`${text}成功`);
          this.getTableList();
        });
      });
    },
    confirmAnnouncement() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true;
          const fn = this.isEdit
            ? AnnouncementApi.editAnnouncement
            : AnnouncementApi.addAnnouncement;
          fn(this.form).then((res) => {
            ElMessage.success("添加成功");
            this.formLoading = false;
            this.dialogVisible = false;
            this.getTableList();
          });
        }
      });
    },
    del(row) {
      ElMessageBox.confirm("确定删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        AnnouncementApi.delAnnouncement({ id: row.id }).then((res) => {
          ElMessage.success("删除成功");
          this.getTableList();
        });
      });
    },
    edit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.isEdit = true;
      this.dialogVisible = true;
    },
    addAnnouncement() {
      this.form = {
        title: "",
        content: "",
        push: 1,
      };
      this.isEdit = false;
      this.dialogVisible = true;
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      this.curPage = val;
      this.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      self.loading = true;
      AnnouncementApi.announcementList(Params, true)
        .then((res) => {
          self.loading = false;
          self.tableData = res.data.records;
          self.totalDataNumber = res.data.total;
        })
        .catch((error) => {});
    },

    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.getTableList();
    },
  },
};
</script>
<style></style>
